package com.leyou.item.controller;

import com.leyou.item.bo.SpuBo;
import com.leyou.item.pojo.Sku;
import com.leyou.item.pojo.SpuDetail;
import com.leyou.common.parameter.SpuQueryByPageParameter;
import com.leyou.common.pojo.PageResult;
import com.leyou.item.service.GoodsService;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("goods")
public class GoodsController {

    @Resource
    private GoodsService goodsService;

    /**
     * 分页查询商品
     * @param page
     * @param rows
     * @param sortBy
     * @param desc
     * @param key
     * @param saleable
     * @return
     */
    @GetMapping("spu/page")
    public ResponseEntity<PageResult<SpuBo>> querySpuByPageAndSort(@RequestParam(name = "page", defaultValue = "1") Integer page,
                                                                   @RequestParam(name = "rows", defaultValue = "5") Integer rows,
                                                                   @RequestParam(name = "sortBy", required = false) String sortBy,
                                                                   @RequestParam(name = "desc", defaultValue = "false") Boolean desc,
                                                                   @RequestParam(name = "key", required = false) String key,
                                                                   @RequestParam(name = "saleable", defaultValue = "true") Boolean saleable) {
        SpuQueryByPageParameter param = new SpuQueryByPageParameter(page, rows, sortBy, desc, key, saleable);
        PageResult<SpuBo> result = goodsService.querySpuboByPageAndSort(param);
        if (result == null) {
            return ResponseEntity.notFound().build();
        }
        return ResponseEntity.ok(result);
    }

    /**
     * 新增商品
     * @param spu
     * @return
     */
    @PostMapping
    public ResponseEntity<Void> saveGoods(@RequestBody SpuBo spu) {
        try {
            goodsService.addGoods(spu);
            return ResponseEntity.ok().build();
        } catch (Exception e) {
            e.printStackTrace();
        }
        //500服务器内部错误
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    /**
     * 商品上下架(单个、批量)
     * @param ids
     * @return
     */
    @PutMapping("spu/out/{id}")
    public ResponseEntity<Void> goodsSoldOutPut(@PathVariable("id") String ids) {
        //separator：分隔符
        String separator = "-";
        //批量
        if (ids.contains(separator)) {
            //split拆分
            String[] goodsId = ids.split(separator);
            for (String id : goodsId) {
                goodsService.goodsSoldOutPut(Long.parseLong(id));
            }
        }
        //单个
        else {
            goodsService.goodsSoldOutPut(Long.parseLong(ids));
        }
        return ResponseEntity.ok().build();
    }


    /**
     * 商品删除(单个、批量)
     * @param ids
     * @return
     */
    @DeleteMapping("spu/{id}")
    public ResponseEntity<Void> deleteGoods(@PathVariable("id") String ids) {
        String separator = "-";
        if (ids.contains(separator)) {
            String[] goodsId = ids.split(separator);
            for (String id : goodsId) {
                goodsService.deleteGoodsById(id);
            }
        } else {
            goodsService.deleteGoodsById(ids);
        }
        return ResponseEntity.ok().build();
    }


    /**
     * 商品修改
     * @param spuBo
     * @return
     */
    @PutMapping
    public ResponseEntity<Void> updateGoods(@RequestBody SpuBo spuBo){
        try {
            goodsService.updateGoods(spuBo);
            return ResponseEntity.ok().build();
        } catch (Exception e) {
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
        }
    }

    /**
     * 商品回显:根据id查询商品信息
     * @param id
     * @return
     */
    @GetMapping("spu/{id}")
    public ResponseEntity<SpuBo> queryGoodsBySpuId(@PathVariable("id") Long id){
        SpuBo spuBo = goodsService.queryGoodsBySpuId(id);
        if (spuBo==null){
            return ResponseEntity.notFound().build();
        }
        return ResponseEntity.ok(spuBo);
    }

    /**
     * 根据spuId查询所有的List<Sku>
     * @param id
     * @return
     */
    @GetMapping("sku/list/{id}")
    public ResponseEntity<List<Sku>> querySkuBySpuId(@PathVariable("id") Long id){
        List<Sku> skus = goodsService.querySkuBySpuId(id);
        if (skus==null||skus.size()==0){
            //404
            return ResponseEntity.notFound().build();
        }
        return ResponseEntity.ok(skus);
    }

    /**
     * 根据spuId查询SpuDetail
     * @param id
     * @return
     */
    @GetMapping("spu/detail/{id}")
    public ResponseEntity<SpuDetail> querySpuDetailBySpuId(@PathVariable("id") Long id){
        SpuDetail spuDetail = goodsService.querySpuDetailBySpuId(id);
        if (spuDetail==null){
            //404
            return ResponseEntity.notFound().build();
        }
        return ResponseEntity.ok(spuDetail);
    }

    /**
     * 根据skuId来查询sku对象
     * @param id
     * @return
     */
    @GetMapping("sku/{id}")
    public  ResponseEntity<Sku> querySkuById(@PathVariable("id") Long id){
        Sku sku = goodsService.querySkuById(id);
        if (sku==null){
            //404
            return ResponseEntity.notFound().build();
        }
        return ResponseEntity.ok(sku);
    }

}
